// pages/setting/PersonalSettings/PersonalSettings.js
const Controller = require('../../controllers/controllers').controller
const request = require('../../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title:'个人设置',
    portrait: '',
    nick: '柯小南不吃糖'
  },
  // 
  nickInput(e) {
    this.setData({
      nick: e.detail.value
    })
  },
  // 切换头像
  changePortrait() {
    let that = this
    console.log('切换头像')
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: function (res) {
        let tempFilePaths = res.tempFilePaths;
        console.log(res, res.tempFilePaths)
        wx.showLoading({
          title: '上传中...',
        })
        wx.uploadFile({
          url: Controller.upImg(),
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            "Content-Type": "multipart/form-data"
          },
          success: function (res) {
            console.log('uploadFile', res, JSON.parse(res.data))
            let success = JSON.parse(res.data);
            if (success.code == 0) {
              that.setData({
                portrait: success.data.baseData[0]
              })
            }
            wx.hideLoading();
          },
          fail: function (res) {
            wx.hideLoading();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) {}
            })
          }
        })
      },
    })
  },
  // 保存按钮
  saveBtn() {
    let that = this
    console.log('保存按钮')
    let params = {
      portrait: this.data.portrait,
      nick: this.data.nick
    }
    Controller.updateInfo(params)
      .then(res => {
        console.log('修改信息', res)
        if (res.code == 0) {
          wx.showToast({
            title: '修改成功',
            duration: 1000
          })
          that.onLoad()
        }
      })
  },
  // 获取用户信息
  getUserInfos() {
    let that = this
    Controller.getUserInfo()
      .then(res => {
        console.log('获取用户信息', res)
        if (res.code == 0) {
          that.setData({
            portrait: res.data.baseData.portrait,
            nick: res.data.baseData.nick
          })
          wx.setStorageSync('userInfo', res.data.baseData)
        }
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getUserInfos();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onShareAppMessage: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·个人设置',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
  onShareTimeline: function (e) {
    // if (e.from == "button") {
      let obj = {
        title: '有氧FM·个人设置',
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
    // }
  },
})